/**
 * 报表设计公司(2011)
 */
package net.abbp.report.view.cell.editor;

import java.util.HashMap;
import java.util.Map;

import net.abbp.engine.ui.model.component.Label;
import net.abbp.engine.ui.model.component.Select;
import net.abbp.engine.ui.model.component.Text;
import net.abbp.report.model.ReportModel;
import net.abbp.report.view.cell.editor.eform.LabelEditor;
import net.abbp.report.view.cell.editor.eform.SelectEditor;
import net.abbp.report.view.cell.editor.eform.TextEditor;
import net.abbp.report.view.cell.value.FormulaValue;
import net.abbp.report.view.cell.value.JsDataValue;

/**
 * 单元格编辑器管理器
 * 
 * @version 1.0 2011-3-22
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
public enum CellEditorManager {
    INSTANCE;

    // 编辑器缓存
    private Map<Class<?>, ICellEditor> editors = new HashMap<Class<?>, ICellEditor>();
    // Action缓存
    private Map<Class<?>, CellEditAction> actionMap = new HashMap<Class<?>, CellEditAction>();

    CellEditorManager() {
        // 报表
        registerCellEditor(String.class, new GeneralCellEditor());
        registerCellEditor(JsDataValue.class, new JsDataValueEditor());
        registerCellEditor(ReportModel.class, new SubReportCellEditor());
        registerCellEditor(FormulaValue.class, new FormulaEditor());

        // 电子表单
        registerCellEditor(Label.class, new LabelEditor());
        registerCellEditor(Text.class, new TextEditor());
        registerCellEditor(Select.class, new SelectEditor());

        // 注册报表Action
        registerCellAction("普通文本", String.class);
        registerCellAction("表格", ReportModel.class);
        registerCellAction("公式", FormulaValue.class);
        registerCellAction("履历信息", JsDataValue.class);

        registerCellAction("标签", Label.class);
        registerCellAction("文本框", Text.class);
        registerCellAction("下拉框", Select.class);
    }

    /**
     * 注册单元格编辑器
     * 
     * @param clazz
     *            单元格类
     * @param cellEditor
     *            单元格编辑器
     */
    private void registerCellEditor(Class<?> clazz, ICellEditor cellEditor) {
        this.editors.put(clazz, cellEditor);
    }

    /**
     * 注册单元格对象操作Action
     * 
     * @param name
     *            名称
     * @param clazz
     *            类
     */
    private void registerCellAction(String name, Class<?> clazz) {
        if (actionMap.get(clazz) != null) {
            return;
        }

        actionMap.put(clazz, new CellEditAction(name, clazz));
    }

    /**
     * 根据类获取编辑器
     * 
     * @param clazz
     *            类
     * @return 编辑器
     */
    public ICellEditor getCellEditor(Class<?> clazz) {
        ICellEditor editor = editors.get(clazz);

        if (editor == null) {
            return editors.get(String.class);
        } else {
            return editor;
        }
    }

    /**
     * 获取单元格对象操作Action
     * 
     * @param clazz
     *            对象类
     * @return Action
     */
    public CellEditAction getCellAction(Class<?> clazz) {
        CellEditAction action = actionMap.get(clazz);
        if (action == null) {
            throw new InternalError("单元格对象编辑操作：" + clazz.getName() + "没有注册!");
        }

        return action;
    }

    /**
     * 根据对象获取编辑器
     * 
     * @param o
     *            对象
     * @return 编辑器
     */
    public ICellEditor getCellEditor(Object o) {
        Class<?> clazz = (o == null ? String.class : o.getClass());

        return this.getCellEditor(clazz);
    }
}
